package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.MyFavoriteArtic;

/**
 * Created by 叶寅 on 2017/9/25.
 */

public class ArticleRecyclerViewAdapter extends RecyclerView.Adapter {

    OnItemClickListener mOnItemClickListener;


    private Context context;
    private List<MyFavoriteArtic.DataMyFavoriteArtic> dataEntityList;

    public ArticleRecyclerViewAdapter(Context context, List<MyFavoriteArtic.DataMyFavoriteArtic> dataEntityList) {
        this.context = context;
        this.dataEntityList = dataEntityList;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_my_favorite_article, null);
        return new ArticleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ArticleViewHolder) holder).setData(position);
    }

    @Override
    public int getItemCount() {
        return dataEntityList.size();
    }


    public void setItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {

        void onCanelCollection(int position);

        void onStartArticleDetails(int position);
    }


    class ArticleViewHolder extends RecyclerView.ViewHolder {

        private ImageView userImage;
        private TextView jiaobiaoNum;
        private TextView mTitle;
        private TextView mContent;
        private Button mDelte;
        private LinearLayout btnStartAticle;


        public ArticleViewHolder(View itemView) {
            super(itemView);

            /**
             * indView(R.id.imageView_article_list)
             SimpleDraweeView imageViewArticleList;
             @BindView(R.id.textView_article_list_num) TextView textViewArticleListNum;
             @BindView(R.id.imageView9) RelativeLayout imageView9;
             @BindView(R.id.textView_article_list_title) TextView textViewArticleListTitle;
             @BindView(R.id.textView_article_list_content) TextView textViewArticleListContent;
             @BindView(R.id.btnDelete) Button btnDelete;
             */
            userImage = (ImageView) itemView.findViewById(R.id.imageView_article_list);
            jiaobiaoNum = (TextView) itemView.findViewById(R.id.textView_article_list_num);
            mTitle = (TextView) itemView.findViewById(R.id.textView_article_list_title);
            mContent = (TextView) itemView.findViewById(R.id.textView_article_list_content);

            mDelte = (Button) itemView.findViewById(R.id.btnDelete);

            btnStartAticle = (LinearLayout) itemView.findViewById(R.id.btn_start_article);
        }


        public void setData(final int position) {
            userImage.setImageURI(Uri.parse(dataEntityList.get(position).getImgUrl()));
            jiaobiaoNum.setText(position + 1 + "");
            mTitle.setText(dataEntityList.get(position).getTitle());
            mContent.setText(dataEntityList.get(position).getContent());

            btnStartAticle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onStartArticleDetails(position);
                }
            });



            mDelte.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onCanelCollection(position);
                }
            });

        }
    }
}
